package org.dromara.module.api;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.ResponseCode;
import org.dromara.common.core.utils.GsonResultBean;
import org.dromara.common.core.utils.Tools;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.TableDataInfoApi;
import org.dromara.module.domain.*;
import org.dromara.module.service.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 *@Author: ZhouWei
 *@Description:  商品相关接口
 *@Date 2018-05-10
 */
@Slf4j
@SaIgnore
@Component
@RestController
@RequestMapping("/api/product")//url:模块/资源/{}/细分
public class ProductApiController {
    @Resource
    private IWxUserService userService;
    @Resource
    private IProductService productService;
    @Resource
    private IProductTypeService productTypeService;
    @Resource
    private IProductAttrValueService productAttrValueService;

    /** 获取商品的详情  old
     * @param
     * @throws Exception
     */
    @PostMapping(value="/detail")
    public JSONObject getDetailInfo1(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        Map<String,Object> map = new HashMap<String, Object>();
        try{
            log.info("product/detail 接收的参数： "+requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            long product_id = jsonRequest.containsKey("product_id") ? jsonRequest.getInt("product_id") : 0;
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;

            //1. Product 商品属性
            Product product = productService.getProductInfoById(product_id);
            if(product == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该商品不存在"),true,true);
            }

            map.put("product",product);

            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功", map),true,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            log.error(e.toString(), e);
            System.out.println("" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /** 获取商品列表
     * @param
     * @throws Exception
     */
    @PostMapping(value="/list")
    public JSONObject list(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            log.info("product/list 接收的参数： " + requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int type_id = jsonRequest.containsKey("type_id") ? jsonRequest.getInt("type_id") : 0;
            int type_child_id = jsonRequest.containsKey("type_child_id") ? jsonRequest.getInt("type_child_id") : 0;
            int pageIndex = jsonRequest.containsKey("pageIndex") ? jsonRequest.getInt("pageIndex") : 0;
            int pageSize = jsonRequest.containsKey("pageSize") ? jsonRequest.getInt("pageSize") : 10;
            String keyword =  jsonRequest.containsKey("keyword") ? jsonRequest.getStr("keyword") : null;

            WxUser user = userService.getById(user_id);

            List<Product> list = productService.queryProductList(user, keyword, type_id, type_child_id, pageIndex, pageSize);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功", list.size(),list),false,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            log.error(e.toString(), e);
            System.out.println(""+e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @description 全部商品
     * @param requestParam 请求参数
     * @author HanTao
     * @date 2022/4/17 14:22
     **/
    @PostMapping(value="/whole/list")
    public JSONObject wholeList(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            log.info("product/list 接收的参数： "+requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int pageIndex = jsonRequest.containsKey("pageIndex") ? jsonRequest.getInt("pageIndex") : 0;
            int pageSize = jsonRequest.containsKey("pageSize") ? jsonRequest.getInt("pageSize") : 10;
            int type_id = jsonRequest.containsKey("type_id") ? jsonRequest.getInt("type_id") : 0;
            int type_child_id = jsonRequest.containsKey("type_child_id") ? jsonRequest.getInt("type_child_id") : 0;

            String keyword = jsonRequest.containsKey("keyword") ? jsonRequest.getStr("keyword") : "";

            WxUser user = userService.getById(user_id);

            //2. 内容行列表
//            List<TitleMenuSection> sections = titleMenuSectionService.selectByMenuDefault(pageIndex,pageSize);
//            sections.forEach(titleMenuSection -> {
//                titleMenuSection.setIsShowTitle(0);
//            });


            Map<String,Object> map = new HashMap<>();
            List<Integer> typeIds = new ArrayList<>();
            int parentId = 0;
            List<ProductType> types = productTypeService.getChildByParentId(parentId);
            if(CollUtil.isNotEmpty(types)){
                for (ProductType type : types){
                    typeIds.add(type.getId());
                }
            }
            map.put("productTypes",types);
            map.put("productTypeIds",typeIds);
            List<Product> products = productService.queryProductList(user, keyword, type_id, type_child_id, pageIndex, pageSize);

            map.put("products",products);

            ProductType type = productTypeService.getById(type_id);
            if(type!= null){
                map.put("typeName",type.getTypeName());
            }

            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功", products.size(),map),false,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            log.error(e.toString(), e);
            System.out.println(""+e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
